import torch
import torch.nn as nn
import torch.nn.functional as nn_functional

from setting import batch_size
import torchinfo


class Model(nn.Module):

    def __init__(self):
        super(Model, self).__init__()

        # [10, 1, 1000, 450]
        self.conv1_1 = nn.Sequential(
            nn.Conv2d(3, 8, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(8),
            nn.ReLU()
        )
        self.conv1_2 = nn.Sequential(
            nn.Conv2d(8, 8, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(8),
            nn.ReLU()
        )
        self.max_pooling1 = nn.MaxPool2d(kernel_size=2, stride=2)
        # [10, 8, 500, 225]
        self.conv2_1 = nn.Sequential(
            nn.Conv2d(8, 16, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(16),
            nn.ReLU()
        )
        self.conv2_2 = nn.Sequential(
            nn.Conv2d(16, 16, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(16),
            nn.ReLU()
        )
        self.max_pooling2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=(0, 1))
        # [10, 16, 250, 113]
        self.conv3_1 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(32),
            nn.ReLU()
        )
        self.conv3_2 = nn.Sequential(
            nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(32),
            nn.ReLU()
        )
        self.max_pooling3 = nn.MaxPool2d(kernel_size=2, stride=2, padding=(0, 1))
        # [10, 32, 125, 57]
        self.conv4_1 = nn.Sequential(
            nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU()
        )
        self.conv4_2 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU()
        )
        self.max_pooling4 = nn.MaxPool2d(kernel_size=2, stride=2, padding=1)
        # [10, 64, 63, 29]
        self.conv5_1 = nn.Sequential(
            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU()
        )
        self.conv5_2 = nn.Sequential(
            nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU()
        )
        self.max_pooling5 = nn.MaxPool2d(kernel_size=2, stride=2, padding=1)
        # [10, 128, 32, 15]
        self.conv6_1 = nn.Sequential(
            nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(256),
            nn.ReLU()
        )
        self.conv6_2 = nn.Sequential(
            nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(256),
            nn.ReLU()
        )
        self.max_pooling6 = nn.MaxPool2d(kernel_size=2, stride=2, padding=(0,1))
        # [10, 256, 16, 8]

        # 完全由全连接成构成
        self.fc = nn.Sequential(
            nn.Flatten(),
            # 隐藏层
            nn.Linear(in_features=256*16*8, out_features=1024),
            nn.ReLU(),
            # 输出层
            nn.Linear(in_features=1024, out_features=2)
        )

    def forward(self, x):
        x = self.conv1_1(x)
        x = self.conv1_2(x)
        x = self.max_pooling1(x)
        x = self.conv2_1(x)
        x = self.conv2_2(x)
        x = self.max_pooling2(x)
        x = self.conv3_1(x)
        x = self.conv3_2(x)
        x = self.max_pooling3(x)
        x = self.conv4_1(x)
        x = self.conv4_2(x)
        x = self.max_pooling4(x)
        x = self.conv5_1(x)
        x = self.conv5_2(x)
        x = self.max_pooling5(x)
        x = self.conv6_1(x)
        x = self.conv6_2(x)
        x = self.max_pooling6(x)
        x = self.fc(x)
        return x


def getModel():
    return Model()


if __name__ == '__main__':
    model = getModel()
    out = model(torch.randn(batch_size, 1, 1000, 450))
    print(out)
    # print(torchinfo.summary(model, (batch_size, 1, 1000, 450)))
